package com.briup.day16;

import java.util.Scanner;

/**
 * @author ThinkBook
 */
public class Test05_StringFunction {
    //replace替换案例
    public static void main(String[] args) {
        //1.实例化sc对象，获取手机字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("input tel: ");
        String tel = sc.nextLine();

        //2.截取字符串中间4位
        String mid = tel.substring(3,3+4);

        //3.用****替换中间4位，得到新tel
        //replace不修改原始字符串，将替换后的新字符串返回
        String newTel = tel.replace(mid,"****");

        //4.输出新tel
        System.out.println("newTel: " + newTel);
    }
    //字符串拆分  001:zs:89
    public static void main03(String[] args) {
        //1.从键盘录入学生信息
        Scanner sc = new Scanner(System.in);
        System.out.println("input string: ");
        String str = sc.nextLine();

        //2.对str进行拆分，分隔符: ':'
        String[] arr = str.split(":");
        System.out.println("数组长度: " + arr.length);

        //3.输出学生基本信息
        System.out.println("id: " + arr[0]);
        System.out.println("name: " + arr[1]);
        //注意，分数为int类型，需要String --> int
        int score = Integer.parseInt(arr[2]);
        //  String --> double
        double s = Double.parseDouble(arr[2]);
        System.out.println("score: " + score);
        System.out.println("s(double): " + s);
    }

    public static void main01(String[] args) {
        //1.实例化sc对象，获取从键盘录入整行字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("input string: ");
        String str = sc.nextLine();

        //2.获取字符串长度 及 索引2上的字符
        System.out.println("length: " + str.length());
        System.out.println("charAt(2): " + str.charAt(2));

        //3.统计字符串中数字字符的个数
        char[] array = str.toCharArray();
        int count = 0;
        //加强for循环，格式：
        //  for (数据类型 变量名 : 数组或集合) {  }
        for (char c : array) {
            if(c >= '0' && c <= '9') {
                count++;
            }
        }
        System.out.println("数字字符个数: " + count);
    }
}
